/**
 * 初始化表格
 * tableId : table id
 * gridparam: 表格参数 除了grid的参数，增加  
 *         baseUrl 基本路径
 *         btnCtl 控制是否显示那些button， 默认值是'add,edit,remove,search'  
 *         dwidth,dheight: 默认弹出对话框的宽度高度                            
 * extToolbar : 数组 表格toobar的额外参数
 * @param parm
 */
function initCurdDatagrid(id,tableId,gridparam,extToolbar)
{
	var obj = {};
	obj.baseUrl = gridparam.baseUrl?gridparam.baseUrl:"";
	obj.jsel = "#" + tableId;
	obj.id = id;
	obj.editDialogId = 'editDialog' + id;
	
	if($('#' + obj.editDialogId).size()<= 0)
	{
		$(document.body).append('<div id="' + obj.editDialogId+ '"></div> ');
	}
	obj.toolbar=[];
	var btnCtl = gridparam.btnCtl!=null?gridparam.btnCtl:'add,edit,remove,search';
	
	obj.dwidth = gridparam.dwidth?gridparam.dwidth:'90%';
	obj.dheight = gridparam.dheight?gridparam.dheight:'90%';
	
	
	if(btnCtl.indexOf('add') >= 0)
	{
		obj.toolbar = obj.toolbar.concat([{ 
	        text: '添加', 
	        iconCls: 'icon-add', 
	        handler: function() { 
	        	obj.edit(0); 
	        } 
	    }, '-']);
	}
	if(btnCtl.indexOf('edit') >= 0)
	{
		obj.toolbar = obj.toolbar.concat([  { 
	        text: '修改', 
	        iconCls: 'icon-edit', 
	        handler: function() { 
	        	obj.edit(1); 
	        } 
	    }, '-']);
	}
	
	if(btnCtl.indexOf('remove') >= 0)
	{
		obj.toolbar = obj.toolbar.concat([{ 
	        text: '删除', 
	        iconCls: 'icon-remove', 
	        handler: function(){ 
	        	obj.del(); 
	        } 
	    }, '-']);
	}
	
	if(btnCtl.indexOf('search') >= 0)
	{
		obj.toolbar = obj.toolbar.concat([   { 
	        text: '查询', 
	        iconCls: 'icon-search', 
	        handler: function(){ 
	        	$("#queryDialog").dialog('open');
	        }
	    } ]);
	}
	if($.isArray(extToolbar))
	{
		obj.toolbar = obj.toolbar.concat(extToolbar);
	}
	obj.defaultOpts = {
			title:'请输入对应的模块', 
			toolbar: obj.toolbar,
	        height: 'auto', 
	        nowrap: true, 
	        striped: true, 
	        border: true, 
	        fitColumns:true,
	        checkOnSelect:false,
	        multiSort:true,
	        collapsible:false,//是否可折叠的 
	        fit: true,//自动大小 
	        pageSize:20,
	        //sortName: 'periodType', 
	        //sortOrder: 'desc', 
	        remoteSort:true,  
	        //idField:'id', 
	        singleSelect:false,//是否单选 
	        pagination:true,//分页控件 
	        rownumbers:true,//行号 
	        frozenColumns:[[ 
	            {field:'id',checkbox:true} 
	        ]], 
		    url:obj.baseUrl + 'listData.do',
		    loadFilter: function(data){
				data.total=data.totalElements;
				data.rows=data.content;
				return data;
			},
		    columns:[[    
		        {field:'test1',title:'测试',width:100}    
		    ]],
		    onDblClickRow:function(index,row){
		    	obj.view(index,row); 
		    }
		};
	obj.gridparam = $.extend({}, obj.defaultOpts, gridparam);
	
	
	obj.view = function(index,row){
		var id = row.id;
		var url = obj.baseUrl + 'view.do?id=' + id;
		$('#'+obj.editDialogId).dialog({    
		    title: '查看信息',    
		    width: obj.dwidth,    
		    height: obj.dheight,      
		    closed: false,    
		    cache: false,    
		    href: url,    
		    modal: true,
		    useIframe:true,
		    callback:function(e){
		    	$(obj.jsel).datagrid('reload')
		    }
		    
		});  
		
	}
	
	obj.edit = function(flag){
		var id = 0;
		if(flag == 1)
		{
			var selArr = $(obj.jsel).datagrid('getSelections');
			if(selArr.length != 1)
			{
				$.messager.alert("验证结果","请选中一行数据",'error');
				return;
			}
			id =  selArr[0].id;
		}
		obj.showSaveOrEdit(id);
	};
	obj.showSaveOrEdit = function(id){
		var url = obj.baseUrl + 'editForm.do';
		var title = '增加信息';
		if(id && id > 0)
		{
			title = '修改信息';
			url = url + "?id=" +id;
		}
		
		$('#'+obj.editDialogId).dialog({    
		    title: title,    
		    width: obj.dwidth,    
		    height: obj.dheight,      
		    closed: false,    
		    cache: false,    
		    href: url,    
		    modal: true,
		    useIframe:true,
		    callback:function(e){
		    	$(obj.jsel).datagrid('reload')
		    }
		    
		});  
	};
	obj.del = function(){
		var selArr = $(obj.jsel).datagrid('getSelections');
		if(selArr.length < 1)
		{
			$.messager.alert("验证结果","请选中一行数据",'error');
			return;
		}
		var ids = '';
		for(var i = 0; i < selArr.length;i++)
		{
			ids += "," + selArr[0].id;
		}
		$.messager.confirm('提示', '您确定要删除吗？',
				function(flag) {
					if (flag) {
						var params = {};
						params.ids = ids.substr(1);
						KD.ajax({
							url : obj.baseUrl + 'delByIds.do',
							data : params,
							success : function(data) {
								$.messager.alert("提示", data.message, "info",function(){
									$(obj.jsel).datagrid('reload');
								});
							}
						});
					}
				});
		
		
	}
	
	$(obj.jsel).datagrid(obj.gridparam);
	return obj;
}


/**
 * 初始化表格
 * tableId : table id
 * gridparam: 表格参数 除了grid的参数，增加  
 *         baseUrl 基本路径
 *         btnCtl 控制是否显示那些button， 默认值是'add,edit,remove'
 *         model : edit, add   如果是edit 会取装载数据，如果是add不会装载。
 *         dwidth,dheight: 默认弹出对话框的宽度高度
 * 			                          
 * extToolbar : 数组 表格toobar的额外参数
 * @param parm
 */
function initCurdDatagrid4Sub(id,tableId,gridparam,extToolbar)
{
	var obj = {};
	obj.baseUrl = gridparam.baseUrl?gridparam.baseUrl:"";
	obj.jsel = "#" + tableId;
	obj.id = id;
	obj.editDialogId = 'editDiv_' + id;
	obj.toolbar=[];
	var btnCtl = gridparam.btnCtl!=null?gridparam.btnCtl:'add,edit,remove';
	if($('#' + obj.editDialogId).size()<= 0)
	{
		$(document.body).append('<div id="' + obj.editDialogId+ '"></div> ');
	}
	
	obj.dwidth = gridparam.dwidth?gridparam.dwidth:'90%';
	obj.dheight = gridparam.dheight?gridparam.dheight:'90%';
	if(btnCtl.indexOf('add') >= 0)
	{
		obj.toolbar = obj.toolbar.concat([{ 
	        text: '添加', 
	        iconCls: 'icon-add', 
	        handler: function() { 
	        	obj.edit(0); 
	        } 
	    }, '-']);
	}
	if(btnCtl.indexOf('edit') >= 0)
	{
		obj.toolbar = obj.toolbar.concat([  { 
	        text: '修改', 
	        iconCls: 'icon-edit', 
	        handler: function() { 
	        	obj.edit(1); 
	        } 
	    }, '-']);
	}
	
	if(btnCtl.indexOf('remove') >= 0)
	{
		obj.toolbar = obj.toolbar.concat([{ 
	        text: '删除', 
	        iconCls: 'icon-remove', 
	        handler: function(){ 
	        	obj.del(); 
	        } 
	    }, '-']);
	}
	 
	if($.isArray(extToolbar))
	{
		obj.toolbar = obj.toolbar.concat(extToolbar);
	}
	obj.defaultOpts = {
			title:'请输入对应的模块', 
			toolbar: obj.toolbar,
	        height: 'auto', 
	        nowrap: false, 
	        striped: true, 
	        border: true, 
	        fitColumns:true,
	        checkOnSelect:false,
	        multiSort:true,
	        collapsible:false,//是否可折叠的 
	        fit: true,//自动大小 
	        //sortName: 'periodType', 
	        //sortOrder: 'desc', 
	        remoteSort:false,  
	        idField:'id', 
	        singleSelect:true,//是否单选 
	        pagination:false,//分页控件 
	        rownumbers:true,//行号 
	        frozenColumns:[[ 
	            {field:'id',checkbox:true} 
	        ]], 
		    url:obj.baseUrl + 'listData.do',
		    loadFilter: function(data){
				data.total=data.totalElements;
				data.rows=data.content;
				return data;
			},
		    columns:[[    
		        {field:'test1',title:'测试',width:100}    
		    ]],
		    onDblClickRow:function(index,row){
		    	obj.view(index,row); 
		    }
		};
	obj.gridparam = $.extend({}, obj.defaultOpts, gridparam);
	//判断是否装载数据
	if(obj.gridparam.model == 'add')
	{
		obj.gridparam.url=null;
	}
	
	obj.edit = function(flag){
		var idx = -1;
		var selObj = null;
		
		if(flag == 1)
		{
			selObj = $(obj.jsel).datagrid('getSelected');
			var idx = $(obj.jsel).datagrid('getRowIndex',selObj);
			if(selObj == null)
			{
				$.messager.alert("验证结果","请选中一行数据",'error');
				return;
			}
		}
		obj.showSaveOrEdit(idx,selObj);
	};
	
	obj.view = function(index,row){
		var id = row.id;
		var url = obj.baseUrl + 'view.do?id=' + id;
		$('#'+obj.editDialogId).dialog({    
		    title: '查看信息',    
		    width: obj.dwidth,    
		    height: obj.dheight,    
		    closed: false,    
		    cache: false,    
		    href: url,    
		    modal: true,
		    useIframe:true,
		    formData: row,
		    gridIdx: index,
		    callback:function(e){
		    	$(obj.jsel).datagrid('reload')
		    }
		    
		});  
		
	}
	obj.showSaveOrEdit = function(idx,data){
		var title = '增加信息';
		var url = obj.baseUrl + 'editForm.do';
		if(idx >=  0 && data != null)
		{
			title = '修改信息';
		}
		$('#'+obj.editDialogId).dialog({    
		    title: title,    
		    width: obj.dwidth,    
		    height: obj.dheight,     
		    closed: false,    
		    cache: false,    
		    href: url,   
		    useIframe:true,//自定义参数，表示是否用iframe来装载对话框
		    modal: true,
		    formData: data,//自定义参数
		    gridIdx: idx,//自定义参数
		    callback:function(data){
		    	if(idx < 0)
		    	{
		    		$(obj.jsel).datagrid('appendRow',data);
		    	}
		    	else
		    	{
		    		$(obj.jsel).datagrid('updateRow',{index:idx,row:data});
		    	}
		    }
		    
		});  
	};
	obj.del = function(){
		var selObj = $(obj.jsel).datagrid('getSelected');
		if(selObj == null)
		{
			$.messager.alert("验证结果","请选中一行数据",'error');
			return;
		}
		var idx = $(obj.jsel).datagrid('getRowIndex',selObj); 
		$(obj.jsel).datagrid('deleteRow',idx);
	}
	
	$(obj.jsel).datagrid(obj.gridparam);
	return obj;
}







/**
 * 初始化可编辑表格表格
 * tableId : table id
 * gridparam: 表格参数 除了grid的参数，增加  
 *         baseUrl 基本路径
 *         model : edit, add   如果是edit 会取装载数据，如果是add不会装载。                        
 * extToolbar : 数组 表格toobar的额外参数
 * @param parm
 */
function initEditableCurdDatagrid4Sub(id,tableId,gridparam,extToolbar)
{
	var obj = {};
	obj.baseUrl = gridparam.baseUrl?gridparam.baseUrl:"";
	obj.jsel = "#" + tableId;
	obj.id = id;
	obj.editDialogId = 'editDiv_' + id;
	var btnCtl = gridparam.btnCtl!=null?gridparam.btnCtl:'add,edit,remove,cancel';
	obj.toolbar=[];
	if(btnCtl.indexOf('add') >= 0)
	{
		obj.toolbar = obj.toolbar.concat([{ 
			text: '添加', 
	        iconCls: 'icon-add', 
	        handler: function() { 
	        	obj.add(); 
	        } 
	    }, '-']);
	}
	if(btnCtl.indexOf('edit') >= 0)
	{
		obj.toolbar = obj.toolbar.concat([{ 
			text: '修改', 
	        iconCls: 'icon-edit', 
	        handler: function() { 
	        	obj.edit(); 
	        } 
	    }, '-']);
	}
	if(btnCtl.indexOf('cancel') >= 0)
	{
		obj.toolbar = obj.toolbar.concat([{ 
			text: '取消编辑', 
	        iconCls: 'icon-cancel', 
	        handler: function() { 
	        	obj.cancel(); 
	        } 
	    }, '-']);
	}
	if(btnCtl.indexOf('remove') >= 0)
	{
		obj.toolbar = obj.toolbar.concat([{ 
			text: '删除', 
	        iconCls: 'icon-remove', 
	        handler: function(){ 
	        	obj.del(); 
	        } 
	    }, '-']);
	}
	if($('#' + obj.editDialogId).size()<= 0)
	{
		$(document.body).append('<div id="' + obj.editDialogId+ '"></div> ');
	}
	 
	if($.isArray(extToolbar))
	{
		obj.toolbar = obj.toolbar.concat(extToolbar);
	}
	obj.defaultOpts = {
			title:'请输入对应的模块', 
			toolbar: obj.toolbar,
	        height: 'auto', 
	        nowrap: false, 
	        striped: true, 
	        border: true, 
	        fitColumns:true,
	        checkOnSelect:false,
	        multiSort:true,
	        collapsible:false,//是否可折叠的 
	        fit: true,//自动大小 
	        //sortName: 'periodType', 
	        //sortOrder: 'desc', 
	        remoteSort:false,  
	        idField:'id', 
	        singleSelect:false,//是否单选 
	        pagination:false,//分页控件 
	        rownumbers:true,//行号 
	        //saveUrl:'',    
	        //updateUrl: '',    
	        //destroyUrl: '',    
	        frozenColumns:[[ 
	            {field:'id',checkbox:true} 
	        ]], 
		    url:obj.baseUrl + 'listData.do',
		    loadFilter: function(data){
				data.total=data.totalElements;
				data.rows=data.content;
				return data;
			},
		    columns:[[    
		        {field:'test1',title:'测试',width:100}    
		    ]] 
		};
	obj.gridparam = $.extend({}, obj.defaultOpts, gridparam);
	//判断是否装载数据
	if(obj.gridparam.model == 'add')
	{
		obj.gridparam.url=null;
	}
	obj.add = function(){
		$(obj.jsel).edatagrid('addRow');
	}
	obj.save = function(){
		$(obj.jsel).edatagrid('saveRow');
	}
	obj.cancel = function(){
		$(obj.jsel).edatagrid('cancelRow');
	}
	obj.edit = function(){
		var idx = -1;
		var selObj = null;
		selObj = $(obj.jsel).edatagrid('getSelected');
		var idx = $(obj.jsel).edatagrid('getRowIndex',selObj);
		if(selObj == null)
		{
			$.messager.alert("验证结果","请选中一行数据",'error');
			return;
		}
		$(obj.jsel).edatagrid('editRow',idx);
	};
	obj.del = function(){
		$(obj.jsel).edatagrid('destroyRow');
	}
	
	$(obj.jsel).edatagrid(obj.gridparam);
	return obj;
}